package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 设备类延期业务有效期属性
 */
public class LocalOrderDelayEffDateAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if (LocalServiceOfferConsts.SERVICE_OFFER_DELAY.equals(data.getServiceOfferId())) {
            KiteOfferInst mainOfferInst = querySceneInstService.getMainOfferInst(data.getSceneInstId());
            if (Objects.nonNull(mainOfferInst)) {
                Date effTime = new Date();
                KiteAttr experienceTimeAttr = inst.getAttr("200004102");
                String experienceTime = experienceTimeAttr.getValue();
                // 三个月体验
                if ("100000159".equals(experienceTime)) {
                    effTime = DateUtils.addMonth(mainOfferInst.getCreateDate(), 3);
                } else if ("100000160".equals(experienceTime)) {
                    effTime = DateUtils.addMonth(mainOfferInst.getCreateDate(), 6);
                }
                String newEffTime = DateUtils.formatDate(mainOfferInst.getCreateDate(), DateUtils.DATE_FORMAT) + "-" + DateUtils.formatDate(effTime, DateUtils.DATE_FORMAT);
                attr.setValue(newEffTime);
                attr.setValueDesc(newEffTime);
            }
        }
        super.set(attr, inst, value, data);
    }
}
